package com.frinder.base.db;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author frinder
 * @date 2018/6/5
 * @className MyJdbcService
 * @package com.kiktech.base.base
 * @Description: jdbc操作类
 */
@Slf4j
@Service
public class MyJdbcService {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    /**
     * @param sql
     * @param queryParams
     * @return
     */
    public List<Map<String, Object>> queryMapList(String sql, Map<String, Object> queryParams) {
        return jdbcTemplate.queryForList(sql, queryParams);
    }

    /**
     * @param sql
     * @param queryParams
     * @param <T>
     * @return
     */
    public <T> List<T> queryList(String sql, Class<T> clazz, Map<String, Object> queryParams) {
        return jdbcTemplate.query(sql, queryParams, new BeanPropertyRowMapper<>(clazz));
    }

    /**
     * @param sql
     * @param queryParameterSource
     * @param <T>
     * @return
     */
    public <T> List<T> queryList(String sql, Class<T> clazz, BeanPropertySqlParameterSource queryParameterSource) {
        return jdbcTemplate.query(sql, queryParameterSource, new BeanPropertyRowMapper<>(clazz));
    }

    /**
     * @param sql
     * @param obj 不支持集合
     * @param <T>
     * @return
     */
    public <T> List<T> queryList(String sql, Class<T> clazz, Object obj) {
        return jdbcTemplate.query(sql, new BeanPropertySqlParameterSource(obj), new BeanPropertyRowMapper<>(clazz));
    }

    /**
     * @param sql
     * @param obj
     * @param <T>
     * @return
     */
    public <T> Optional<T> queryOne(String sql, Class<T> clazz, Object obj) {
        try {
            return Optional.ofNullable(
                    jdbcTemplate.queryForObject(sql,
                            new BeanPropertySqlParameterSource(obj),
                            new BeanPropertyRowMapper<>(clazz)
                    )
            );
        } catch (Exception e) {
            return Optional.empty();
        }
    }

    /**
     * 查询，返回需要类型
     *
     * @param sql
     * @param queryParams
     * @param requiredTypeClass
     * @param <T>
     * @return
     */
    public <T> T queryOne(String sql, Map<String, Object> queryParams, Class<T> requiredTypeClass) {
        try {
            return jdbcTemplate.queryForObject(sql, queryParams, requiredTypeClass);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param sql
     * @param queryParams
     * @param <T>
     * @return
     */
    public <T> T queryOne(String sql, Class<T> clazz, Map<String, Object> queryParams) {
        try {
            return jdbcTemplate.queryForObject(sql, queryParams, new BeanPropertyRowMapper<>(clazz));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param sql
     * @param queryParameterSource
     * @return
     */
    public Map<String, Object> queryMap(String sql, BeanPropertySqlParameterSource queryParameterSource) {
        return jdbcTemplate.queryForMap(sql, queryParameterSource);
    }

    /**
     * @param sql
     * @param queryParams
     * @return
     */
    public Map<String, Object> queryMap(String sql, Map<String, Object> queryParams) {
        return jdbcTemplate.queryForMap(sql, queryParams);
    }

    /**
     * @param list
     */
    public void batchUpdate(String sql, List<?> list) {
        jdbcTemplate.batchUpdate(sql, SqlParameterSourceUtils.createBatch(list.toArray()));
    }

    /**
     * @param sql
     * @param updateParameterSource
     */
    public void update(String sql, MapSqlParameterSource updateParameterSource) {
        jdbcTemplate.update(sql, updateParameterSource);
    }

    /**
     * @param sql
     * @param paramMap
     */
    public void update(String sql, Map<String, Object> paramMap) {
        jdbcTemplate.update(sql, paramMap);
    }

}
